<template>
  <q-page class="q-pa-md">
    <div class="row items-center">
      <div class="q-pb-md text-h5">
        颜色
        <span class="text-subtitle1">本页展示预定义的颜色</span>
      </div>
    </div>
    <div class="q-pa-md shadow-3 row">
      <div class="col-12 q-pb-md row items-center">
        <div :class="'shadow-1 brand-color-grid simple-border '+ chosenBgColorClass"></div>

        <q-input ref="txtChosenBgColorClass" class="q-ml-md" square v-model="chosenBgColorClass" dense readonly>
          <template v-slot:append>
            <q-icon name="file_copy" @click="copyBgColorClassToClipboard()" class="cursor-pointer" />
          </template>
        </q-input>

        <div :class="'shadow-1 q-ml-md q-px-sm color-info-text text-bold '+chosenTextColorClass">测试文本Abc123</div>
        <q-input ref="txtChosenTextColorClass" class="q-ml-md" square v-model="chosenTextColorClass" dense readonly>
          <template v-slot:append>
            <q-icon name="file_copy" @click="copyTextColorClassToClipboard()" class="cursor-pointer" />
          </template>
        </q-input>
      </div>
      <div class="col-12 q-pb-md row">
        <div v-for="color in brandColors" :key="color" :class="'color-grid brand-color-grid simple-border bg-'+color" @click="showColorInfo(color)">
        </div>

      </div>
      <div class="col-12 column color-bar" v-for="colorGroup in colorGroups" :key="colorGroup">
        <div :class="'col color-grid bg-'+colorGroup" @click="showColorInfo(colorGroup)"></div>
        <div v-for="n in 14" :key="n" :class="'col color-grid bg-'+colorGroup+'-'+ n" @click="showColorInfo(colorGroup+'-'+n)">
        </div>
      </div>
    </div>
  </q-page>
</template>

<script lang="ts">
export default {
  name: 'Color'
}
</script>
<script lang="ts" setup>

import {computed, ref} from 'vue'
import {QInput, copyToClipboard } from 'quasar'

let chosenColorClass = ref('white')
const brandColors = [
  'primary',
  'secondary',
  'accent',
  'positive',
  'negative',
  'info',
  'warning',
  'white',
  'black'
]
const colorGroups = [
  'red',
  'pink',
  'purple',
  'deep-purple',
  'indigo',
  'blue',
  'light-blue',
  'cyan',
  'teal',
  'green',
  'light-green',
  'lime',
  'yellow',
  'amber',
  'orange',
  'deep-orange',
  'brown',
  'grey',
  'blue-grey'
]

const showColorInfo = function (color:string) {
  chosenColorClass.value = color
}

const showCopyToClipboardNotify = function () {
  $f.ui.showNotifySucceeded('复制成功')
}

const copyBgColorClassToClipboard = function () {
  copyToClipboard(txtChosenBgColorClass.value?.modelValue as string)
    .then(() => {
      showCopyToClipboardNotify()
    })
    .catch(() => {
      // 失败
    })
}
const copyTextColorClassToClipboard = function () {
  copyToClipboard(txtChosenTextColorClass.value?.modelValue as string)
    .then(() => {
      showCopyToClipboardNotify()
    })
}

const txtChosenBgColorClass = ref<QInput>()
const txtChosenTextColorClass = ref<QInput>()

let chosenBgColorClass = computed(() => {
  return 'bg-' + chosenColorClass.value
})

let chosenTextColorClass = computed(() => {
  return 'text-' + chosenColorClass.value
})

</script>

<style lang="scss" scoped>
$color-grid-side-len: 40px;

.color-bar {
  width: $color-grid-side-len;
  height: $color-grid-side-len * 14;
}

.color-grid {
  cursor: pointer;
}

.brand-color-grid {
  width: $color-grid-side-len;
  height: $color-grid-side-len;
}

.color-info-text {
  line-height: $color-grid-side-len;
}
</style>
